<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;

class StoreController extends Controller
{
    /**
     * store应用入口页面
     * 用于微信公众号中访问UniApp编译后的store项目
     */
    public function index(Request $request)
    {
        // 获取微信相关参数
        $code = $request->get('code');
        $state = $request->get('state');
        
        // 生成随机字符串用于微信JS-SDK
        $nonceStr = Str::random(16);
        $timestamp = time();
        
        // 记录访问日志
        \Log::info('store应用访问', [
            'ip' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'code' => $code,
            'state' => $state,
            'nonceStr' => $nonceStr,
            'timestamp' => $timestamp
        ]);
        
        // 读取打包后的 store index.html（当前放置于 public/store）
        $storeIndexPath = public_path('store/index.html');
        if (!file_exists($storeIndexPath)) {
            \Log::error('store index.html 文件不存在', ['path' => $storeIndexPath]);
            return response('store应用未构建，请先在 hhm_store 执行构建', 404);
        }
        $html = file_get_contents($storeIndexPath);
        
        return response($html)->header('Content-Type', 'text/html; charset=utf-8');
    }
}
